# data = {'name':'qiku','age':10,'addr':'金水'}
# url_encode = parse.urlencode(data)
# print(url_encode)

# url = f"http://httpbin.org/get?{url_encode}"
# req = Request(url,headers={
# 'user-agent':
# 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1'
# },method='GET')
#
# response = request.urlopen(req)
# if response.status == 200:
#     response_bytes = response.read()
#     response_text = response_bytes.decode('utf-8')
#     response_json = json.loads(response_text)
#     print(response_json['headers']['User-Agent'],response_json['args'])



# url = "http://httpbin.org/post?name=qiku"
#'content-type':'application/x-www-form-urlencoded',
# data = {'age':10,'addr':'金水'}
# json_encode = json.dumps(data)
#
#
# # 1.2 构建请求实例
# req = Request(url,data=json_encode.encode(), headers={
# 'user-agent':
# 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
#
# # 更改请求参数的类型json
# 'content-type':'application/json',
#
# },method='POST')
# # 2.发起请求 get
# response = request.urlopen(req)
# # 3.解析数据
# if response.status == 200:
#     response_bytes = response.read()
#     response_text = response_bytes.decode('utf-8')
#     response_json = json.loads(response_text)
#     print(response_json,response_json['headers']['User-Agent'],response_json['args'])










import requests

# 1.分析请求地址
# url = "http://httpbin.org/get?name=qiku"
# # 2.发起请求
# response = requests.get(url,params={
#     'age':10,
#     'addr':"东三街"
# })
# # 3.解析
#
# if response.status_code == 200:
#     # print(response.content)
#     # print(response.text)
#     print(response.json())


# url = "http://httpbin.org/get?name=qiku"
#
# response = requests.get(url,params={
#     'age':10,
# },headers={
#     'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
#
# })
# if response.status_code == 200:
#     # print(response.content)
#     # print(response.text)
#     print(response.json())


#
# url = "http://httpbin.org/post?name=qiku"
#
# response = requests.post(url,headers={
# 'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
#
# },data = {
#     'age':10,
#     'addr':'地球村',
#
# }  )
# if response.status_code == 200:
#     print(response.content)
#     print(response.text)
#     print(response.json())




# url = "http://httpbin.org/post?name=qiku"
#
# response = requests.post(url,headers={
# 'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
#
# },json = {
#     'age':10,
#     'addr':'地球村',
# }  )
# if response.status_code == 200:
#     print(response.json())







# url = "http://127.0.0.1:8000/admin/"
# response = requests.get(url,allow_redirects=False)
#
#
# print(response.status_code)
#
# location = response.headers.get('location')
# location = f'http://127.0.0.1:8000{location}'
# response = requests.get(location)
# print(response.status_code)
# print(response.text)



# url = "http://127.0.0.1:8000/admin/"
# headers = {
#     # 携带cookie
#     # 'cookie':'sessionid=ttl8ntmuswx8feft412htfmfvl9i8xm6'
# }
# cookies = {
# 'sessionid':'ttl8ntmuswx8feft412htfmfvl9i8xm6'
# }
# response = requests.get(url,headers=headers,cookies=cookies)
# print(response.status_code)
# print(response.text)


# 使用会话 模拟登录
login_url = "http://127.0.0.1:8000/admin/login/?next=/admin/login"
session = requests.Session()
print(session.cookies)
response = session.get(login_url)
print(session.cookies)

csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">', response.text).group(1)

print(csrfmiddlewaretoken)


response = session.post(login_url,data={
    "username":"admin","password":"123456",
'csrfmiddlewaretoken': csrfmiddlewaretoken,
},)
print(response.text)
